package com.ruoyi.graduate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.graduate.domain.entity.MsCollection;
import com.ruoyi.graduate.domain.param.MsCollectionAddParam;
import com.ruoyi.graduate.mapper.MsCollectionMapper;
import com.ruoyi.graduate.mapping.MsCollectionMapping;
import com.ruoyi.graduate.service.IMsCollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class IMsCollectionServiceImpl extends ServiceImpl<MsCollectionMapper, MsCollection>
implements IMsCollectionService {

    @Autowired
    private MsCollectionMapper msCollectionMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult addMsCollection(MsCollectionAddParam msCollection) {

        MsCollection add = MsCollectionMapping.INSTANCE.add(msCollection);

        QueryWrapper qw = new QueryWrapper();
        qw.eq("user_id", add.getUserId());
        qw.eq("package_id", add.getPackageId());

        MsCollection msCollection1 = msCollectionMapper.selectOne(qw);

        if (msCollection1 != null) {
            int i = msCollectionMapper.deleteById(msCollection1.getCollectionId());

            return i > 0 ? AjaxResult.success("取消收藏成功") : AjaxResult.error();
        }

        int insert = msCollectionMapper.insert(add);


        return insert == 1 ? AjaxResult.success("收藏成功") : AjaxResult.error();
    }

    @Override
    public AjaxResult getList(Long id) {

        return AjaxResult.success(msCollectionMapper.listvo(id));
    }
}
